package com.anysoft.utils;

import android.content.Context;
import android.text.TextUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by LiTingYao on 2018/5/25.
 */
public class StringUtil {
    /**
     * 判断字符串是否为null或长度为0
     *
     */
    public static boolean isEmpty(CharSequence s) {
        return s == null || s.length() == 0;
    }
    
    /**
     * 判断字符串是否是字母和数字的组合
     *
     * @param str
     * @return
     */
    public static boolean strIsLetterOrNumer(String str) {
        boolean flag = false;
        if (isEmpty(str)) {
            String check = "^[A-Za-z0-9]{1,}$";
            Pattern pattern = Pattern.compile(check);
            Matcher matcher = pattern.matcher(str);
            flag = matcher.matches();
        }
        return flag;
    }
    
    /**
     * 用户名必须是数字或者字母的结合
     *
     * @param username 字符串
     * @return boolean
     */
    public static boolean isLegalUsername(String username) {
        for (int i = 0; i < username.length(); i++) {
            char ch = username.charAt(i);
            if (!isAscii(ch) && ch != '.' && ch != '_' && ch != '-'
                    && ch != '+' && ch != '(' && ch != ')' && ch != '*'
                    && ch != '^' && ch != '@' && ch != '%' && ch != '$'
                    && ch != '#' && ch != '~' && ch != '-') {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 字符是否是ascil码
     *
     * @param ch 字符
     * @return boolean
     */
    private static boolean isAscii(char ch) {
        return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
                || (ch >= '0' && ch <= '9');
    }
    
    /**
     * 判断是否包含中文
     *
     * @param str
     * @return
     */
    public static boolean isContainsChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find()) {
            flg = true;
        }
        return flg;
    }
    
    /**
     * 赋值到粘贴板
     * @param mContext
     * @param stripped
     */
    public static void copy(Context mContext, String stripped) {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText("content", stripped);
        clipboard.setPrimaryClip(clip);
    }
    
    public static String getMD5(String s) {
        try {
            // Create MD5 Hash
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();
            
            return toHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        
        return "";
    }
    
    // String to byte
    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
            sb.append(HEX_DIGITS[b[i] & 0x0f]);
        }
        return sb.toString();
    }
    
    private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F'};
    
    public static String removePointString(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        } else {
            return string.replace(".", "");
        }
    }
}
